package wangxu

import (
	"context"
	"github.com/beherochuling/beesoft_api/app/dao"
	"github.com/beherochuling/beesoft_api/app/model/entity"
	model "github.com/beherochuling/beesoft_api/app/model/wangxu"
	"github.com/beherochuling/beesoft_api/app/service/base"
	"github.com/google/uuid"
)

var Letsview = letsviewService{}

type letsviewService struct{}

// Start 发起邀请
// {"action":"startShare","data":{"target":"935978164","pro_id":"114"}}
func (r *letsviewService) Start(ctx context.Context) {
	var productId uint = 1 // "pro_id" // wangxu.DefaultProductId
	code := Model.Code(ctx)

	var targetCode uint64 = 1
	targetUser := NewLetsviewUser(ctx).GetByCode(targetCode)
	user := NewLetsviewUser(ctx).GetByCode(code)

	targetProductId := targetUser.ProductId

	if targetCode == 0 || targetProductId == 0 || productId == 0 {
		return
	}
	if !base.VersionGroup.IsSameProduct(targetProductId, productId) { // 非同系列
		return
	}

	var targetConnection *entity.LetsviewConnections
	err := dao.LetsviewConnections.Ctx(ctx).
		Where(dao.LetsviewConnections.Columns().Code, targetCode).
		Scan(&targetConnection)
	if err != nil {
		panic(err)
	}
	if targetConnection == nil || targetConnection.Fd == 0 { // 对方不在线

	}
	if code == targetCode { // 对方是自己

	}
	isMirror := false
	if isMirror { // 对方正在投屏

	}
	// 对方在后台

	if user.Region == model.RegionOversea && targetUser.Region != model.RegionOversea || user.Region != model.RegionOversea && targetUser.Region == model.RegionOversea {
		return // cross region not allow // 境内外不能互投
	}

	name := uuid.New().String()
	_, err = dao.LetsviewRooms.Ctx(ctx).
		Insert(entity.LetsviewRooms{
			Name:      name,
			ProductId: productId,
			Uid:       user.UserId,
			Code:      user.Code,
			Hash:      user.Hash,
			JoinUid:   targetUser.UserId,
			JoinCode:  targetUser.Code,
			JoinHash:  targetUser.Hash,
			// Starttime: ,
			// Endtime: ,
			// Status: ,
			// MirrorStatus: ,
			// IsCost: ,
			// Heart: ,
			// LastCost: ,
			// Durations: ,
		})
	if err != nil {
		panic(err)
	}

	// todo 发送邀请给目标
	return
	// net.NewResponse(r).ResponseOk()
}

// Stop 停止投屏
// {"action":"stopShare","data":{"channel_name":"cf344fa8-35d6-3bff-ea48-2ae21f92920f"}}
func (r *letsviewService) Stop() {

}

// Accept 同意投屏
// {"action":"acceptShare","data":{"channel_name":"cf344fa8-35d6-3bff-ea48-2ae21f92920f"}}
func (r *letsviewService) Accept() {

}

// Reject 拒绝投屏
// {"action":"refuseShare","data":{"channel_name":"cf344fa8-35d6-3bff-ea48-2ae21f92920f"}}
// todo 暂无逻辑
func (r *letsviewService) Reject() {
}

// JoinSuccess 加入成功
// {"action":"joinSuccess","data":{"channel_name":"cf344fa8-35d6-3bff-ea48-2ae21f92920f"}}
func (r *letsviewService) JoinSuccess() {

}

// JoinFailure 加入失败
// {"action":"joinFailed","data":{"channel_name":"1888b3b5-755f-39da-8af8-bde816bd6553"}}
// todo 暂无逻辑
func (r *letsviewService) JoinFailure() { //
}

// ReJoin 重新加入
// {"action":"rejoin","data":{"channel_name":"1888b3b5-755f-39da-8af8-bde816bd6553"}}
func (r *letsviewService) ReJoin() { //
}

// ChangeStatus 切换状态 0[default]前台1后台
// {"action":"changeStatus","data":{"online_status":"1"}}
func (r *letsviewService) ChangeStatus() { //
}

// CheckCode 检测状态
// {"action":"checkCode","data":{"target":"730968247"}}
func (r *letsviewService) CheckCode() { //
}

// StopNotice 剩余时长不足客户端断开投屏携带投屏时长告知服务端 扣费
// {"action":"stopNotice","data":{"channel_name":"cf344fa8-35d6-3bff-ea48-2ae21f92920f","time":"100"}}
func (r *letsviewService) StopNotice() { //
}

// Heartbeat 心跳
// ping 55s
func (r *letsviewService) Heartbeat() { //
}
